package cn.px.rest.controller.purchase;

import cn.px.common.bean.AbstractPageResultBean;
import cn.px.common.util.BeanConverterUtil;
import cn.px.common.util.R;
import cn.px.common.validation.group.AddGroup;
import cn.px.common.validation.group.UpdateGroup;
import cn.px.dao.dto.purchase.SupplierDTO;
import cn.px.dao.entity.purchase.Supplier;
import cn.px.dao.util.ExcelUtil;
import cn.px.dao.util.SelectUtil;
import cn.px.dao.vo.export.purchase.ExportSupplierVO;
import cn.px.dao.vo.query.purchase.SupplierVO;
import cn.px.rest.log.annotation.SysLog;
import cn.px.service.purchase.SupplierService;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @Description 供应商 控制器
 * @author 品讯科技
 * @date 2024-08
 * @Version Copyright (c) 2020,北京乾元坤和科技有限公司 All rights reserved.
 */
@RestController
@AllArgsConstructor
@RequestMapping("/purchase/suppliers")
public class SupplierController {

    private final SupplierService supplierService;

    /**
     * 通过id查询单条记录
     *
     * @param id 主键
     * @return R
     */
    @GetMapping("/{id}")
    public R<Supplier> getById(@PathVariable("id") String id) {
        return new R<>(supplierService.getModelById(id));
    }

    /**
     * 新增记录
     *
     * @param supplierDTO 供应商DTO
     * @return R
     */
    @SysLog("新增供应商")
    @PostMapping
//  @PreAuthorize("@pms.hasPermission('purchase-suppliers-add')")
    public R save(@Validated({AddGroup.class}) @RequestBody SupplierDTO supplierDTO) {
        return new R<>(supplierService.saveModelByDTO(supplierDTO));
    }

    /**
     * 提交申请
     *
     * @param id id
     * @return R
     */
    @SysLog("提交申请")
    @PutMapping("/{id}/submit")
    public R submit(@PathVariable("id") String id) {
        supplierService.submitAudit(id);
        return new R<>();
    }

    /**
     * 审核
     *
     * @param id id
     * @return R
     */
    @SysLog("审核")
    @PutMapping("/{id}/audit")
    public R audit(@PathVariable("id") String id, @RequestBody SupplierDTO supplierDTO) {
        supplierDTO.setId(id);
        supplierService.audit(supplierDTO);
        return new R<>();
    }

    /**
     * 禁用
     *
     * @param id id
     * @return R
     */
    @SysLog("禁用")
    @PutMapping("/{id}/close")
    public R close(@PathVariable("id") String id, @RequestBody SupplierDTO supplierDTO) {
        supplierDTO.setId(id);
        supplierService.close(supplierDTO);
        return new R<>();
    }

    /**
     * 修改记录
     *
     * @param supplierDTO 供应商DTO
     * @return R
     */
    @SysLog("修改供应商")
    @PutMapping("/{id}")
//  @PreAuthorize("@pms.hasPermission('purchase-suppliers-edit')")
    public R update(@PathVariable("id") String id, @Validated({UpdateGroup.class}) @RequestBody SupplierDTO supplierDTO) {
        supplierService.modModelByDTO(supplierDTO);
        return new R<>();
    }

    /**
     * 通过id删除一条记录
     *
     * @param id 主键
     * @return R
     */
    @SysLog("删除供应商")
    @DeleteMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('purchase-suppliers-del')")
    public R removeById(@PathVariable String id) {
        supplierService.deleteByPrimaryKey(id);
        return new R<>();
    }

    /**
     * 批量记录
     *
     * @param ids 主键
     * @return R
     */
    @SysLog("批量删除供应商")
    @DeleteMapping("")
    @PreAuthorize("@pms.hasPermission('purchase-suppliers-del')")
    public R removeIds(@RequestBody List<String> ids) {
        supplierService.batchDelete(ids);
        return new R<>();
    }

    /**
     * 获取分页
     *
     * @param pageNum     当前页码
     * @param pageSize    每页条数
     * @param supplierDTO 供应商DTO
     * @return R
     */
    @GetMapping("/page")
    public R<AbstractPageResultBean<SupplierVO>> query(SupplierDTO supplierDTO, @RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "20") int pageSize) {
        return new R<>(supplierService.selectPageList(supplierDTO, pageNum, pageSize));
    }

    /**
     * 获取列表
     *
     * @param supplierDTO 供应商DTO
     * @return R
     */
    @GetMapping("")
    public R query(SupplierDTO supplierDTO) {
        return new R<>(supplierService.selectList(supplierDTO));
    }

    /**
     * 供应商导出
     *
     * @param supplierDTO 供应商DTO
     * @return R
     */
    @GetMapping("/export")
    @SysLog("供应商导出")
    @PreAuthorize("@pms.hasPermission('purchase-suppliers-export')")
    public void export(HttpServletResponse response, SupplierDTO supplierDTO) {
        List<Supplier> list = supplierService.getModelListByDTO(supplierDTO);
        ExcelUtil.exportResponseDict(response, ExportSupplierVO.class, BeanConverterUtil.copyListProperties(list, ExportSupplierVO.class), "供应商");
    }

    /**
     * 获取下拉列表
     *
     * @param supplierDTO 生产商DTO
     * @return R
     */
    @GetMapping("options")
    public R queryOptions(SupplierDTO supplierDTO) {
        return new R<>(SelectUtil.buildSupplierOptions(supplierService.getModelListByDTO(supplierDTO)));
    }

    /**
     * 导入
     *
     * @return R
     */
    @PostMapping("/import")
    @SysLog("导入供应商")
    public R importExcel(@RequestParam("file") MultipartFile file) throws Exception {
        supplierService.importSuppliers(file);
        return R.success("导入数据正在处理，请稍后刷新核对数据");
    }

}
